<template>
  <div class="items-page">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">道具图鉴</h1>
      <p class="page-subtitle">探索宝可梦世界中的各种实用道具</p>
    </div>
    
    <!-- 搜索和筛选区域 -->
    <div class="search-filter-section">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <!-- 分类目录按钮 -->
        <div class="category-dropdown">
          <input 
            type="checkbox" 
            id="category-dropdown-checkbox" 
            class="category-dropdown-checkbox"
            v-model="categoryDropdownOpen"
            aria-label="切换分类目录"
            aria-expanded="false"
          >
          <label for="category-dropdown-checkbox" class="category-dropdown-label">
            <el-icon class="category-icon"><Menu /></el-icon>
            <span class="category-text">分类</span>
            <el-icon class="dropdown-arrow" :class="{ 'dropdown-arrow-open': categoryDropdownOpen }"><ArrowDown /></el-icon>
          </label>
          <div class="category-dropdown-menu" v-show="categoryDropdownOpen">
            <div class="category-menu-header">道具分类</div>
            <ul class="category-menu-list">
              <li v-for="category in itemCategories" :key="category.type" class="category-menu-item">
                <a 
                  href="#" 
                  @click.prevent="scrollToCategory(category.type)"
                  class="category-link"
                >
                  <span class="category-emoji">{{ category.icon }}</span>
                  <span class="category-name">{{ category.name }}</span>
                  <span class="category-count">({{ category.count }})</span>
                </a>
              </li>
            </ul>
          </div>
        </div>
        
        <el-input
          v-model="searchQuery"
          placeholder="搜索道具名称..."
          size="large"
          clearable
          @input="handleSearch"
          @clear="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button 
          type="primary" 
          size="large" 
          @click="handleSearch"
          :loading="loading"
        >
          搜索
        </el-button>
        <el-button 
          type="default" 
          size="large" 
          @click="resetFilters"
          class="reset-btn"
        >
          重置
        </el-button>
      </div>
    </div>
    
    <!-- 结果统计 -->
    <div class="result-info" v-if="!loading">
      <span class="result-count">
        共找到 <strong>{{ totalCount }}</strong> 个道具
      </span>
      <span class="current-showing" v-if="filteredItemsList.length > 0">
        当前显示第 {{ (currentPage - 1) * pageSize + 1 }} - {{ Math.min(currentPage * pageSize, filteredItemsList.length) }} 个
      </span>
    </div>
    
    <!-- 道具表格 -->
    <div class="items-table-container" v-loading="loading">
      <!-- 按分类显示道具 -->
      <div v-if="!searchQuery.trim()" class="category-sections">
        <div v-for="category in itemCategories" :key="category.type" :id="`category-${category.type}`" class="category-section">
          <div class="category-header">
            <span class="category-icon">{{ category.icon }}</span>
            <h3 class="category-title">{{ category.name }}</h3>
            <span class="category-count">({{ category.count }}个)</span>
          </div>
          <el-table
            :data="getItemsByCategory(category.type)"
            stripe
            border
            style="width: 100%; margin-bottom: 30px;"
            :header-cell-style="{ background: '#409EFF', color: 'white', fontWeight: 'bold' }"
            :row-style="{ background: 'rgba(255, 255, 255, 0.95)' }"
            @row-click="showItemDetail"
            class="items-table"
          >
            <el-table-column prop="icon" label="" width="60" align="center">
              <template #default="scope">
                <div class="item-icon">{{ scope.row.icon }}</div>
              </template>
            </el-table-column>
            
            <el-table-column prop="chineseName" label="中文" min-width="120" />
            
            <el-table-column prop="japaneseName" label="日文" min-width="150" />
            
            <el-table-column prop="englishName" label="英文" min-width="150" />
            
            <el-table-column prop="description" label="道具说明" min-width="300">
              <template #default="scope">
                <div class="description-text">{{ scope.row.description }}</div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      
      <!-- 搜索结果表格 -->
      <el-table
        v-else
        :data="paginatedItemsList"
        stripe
        border
        style="width: 100%"
        :header-cell-style="{ background: '#409EFF', color: 'white', fontWeight: 'bold' }"
        :row-style="{ background: 'rgba(255, 255, 255, 0.95)' }"
        @row-click="showItemDetail"
        class="items-table"
      >
        <el-table-column prop="icon" label="" width="60" align="center">
          <template #default="scope">
            <div class="item-icon">{{ scope.row.icon }}</div>
          </template>
        </el-table-column>
        
        <el-table-column prop="chineseName" label="中文" min-width="120" />
        
        <el-table-column prop="japaneseName" label="日文" min-width="150" />
        
        <el-table-column prop="englishName" label="英文" min-width="150" />
        
        <el-table-column prop="description" label="道具说明" min-width="300">
          <template #default="scope">
            <div class="description-text">{{ scope.row.description }}</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 空状态 -->
    <div v-if="!loading && filteredItemsList.length === 0" class="empty-state">
      <div class="empty-icon">📦</div>
      <h3>未找到道具</h3>
      <p>请尝试调整搜索条件</p>
      <el-button @click="resetFilters" type="primary" class="reset-primary-btn">重置搜索</el-button>
    </div>
    
    <!-- 分页 - 只在搜索模式下显示 -->
    <div class="pagination-section" v-if="searchQuery.trim() && filteredItemsList.length > 0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handlePageChange"
        @size-change="handlePageSizeChange"
      />
    </div>
    
    <!-- 道具详情对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="selectedItem?.chineseName || '道具详情'"
      width="500px"
      center
    >
      <div v-if="selectedItem" class="item-detail">
        <div class="detail-header">
          <div class="detail-icon">{{ selectedItem.icon }}</div>
          <div class="detail-basic">
            <h2 class="detail-name">{{ selectedItem.chineseName }}</h2>
            <div class="detail-names">
              <p><strong>日文:</strong> {{ selectedItem.japaneseName }}</p>
              <p><strong>英文:</strong> {{ selectedItem.englishName }}</p>
            </div>
          </div>
        </div>
        
        <div class="detail-description">
          <h4>道具说明</h4>
          <p>{{ selectedItem.description }}</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted, watch, nextTick } from 'vue'
// 移除了所有提示
import { Search, Menu, ArrowDown } from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const itemsList = ref([])
const filteredItemsList = ref([])
const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(20)
const dialogVisible = ref(false)
const selectedItem = ref(null)
const categoryDropdownOpen = ref(false)

// 模拟道具数据（基于您提供的表格）
const mockItemsData = [
  {
    id: 1,
    icon: '🥫',
    chineseName: '除虫喷雾',
    japaneseName: 'むしよけスプレー',
    englishName: 'Repel',
    description: '使用后，在较短的一段时间内，弱小的野生宝可梦将完全不会出现。',
    type: 'repel'
  },
  {
    id: 2,
    icon: '🥫',
    chineseName: '白银喷雾',
    japaneseName: 'シルバースプレー',
    englishName: 'Super Repel',
    description: '弱小的野生宝可梦将完全不会出现，效果比除虫喷雾更持久。',
    type: 'repel'
  },
  {
    id: 3,
    icon: '🥫',
    chineseName: '黄金喷雾',
    japaneseName: 'ゴールドスプレー',
    englishName: 'Max Repel',
    description: '弱小的野生宝可梦将完全不会出现，效果比白银喷雾更持久。',
    type: 'repel'
  },
  {
    id: 4,
    icon: '📦',
    chineseName: '木箱',
    japaneseName: 'きのはこ',
    englishName: 'Normal Box',
    description: '打开看看。',
    type: 'ball'
  },
  {
    id: 5,
    icon: '📦',
    chineseName: '桐木箱',
    japaneseName: 'きりのはこ',
    englishName: 'Gorgeous Box',
    description: '打开看看。',
    type: 'ball'
  },
  {
    id: 6,
    icon: '🎵',
    chineseName: '蓝色玻璃哨',
    japaneseName: 'あおいビードロ',
    englishName: 'Blue Flute',
    description: '以蓝色玻璃制成的哨子，可以治愈睡眠状态。',
    type: 'flute'
  },
  {
    id: 7,
    icon: '🎵',
    chineseName: '黄色玻璃哨',
    japaneseName: 'きいろビードロ',
    englishName: 'Yellow Flute',
    description: '以黄色玻璃制成的哨子，可以治愈混乱状态。',
    type: 'flute'
  },
  {
    id: 8,
    icon: '🎵',
    chineseName: '红色玻璃哨',
    japaneseName: 'あかいビードロ',
    englishName: 'Red Flute',
    description: '以红色玻璃制成的哨子，可以治愈着迷状态。',
    type: 'flute'
  },
  {
    id: 9,
    icon: '🎵',
    chineseName: '黑色玻璃哨',
    japaneseName: 'くろいビードロ',
    englishName: 'Black Flute',
    description: '以黑色玻璃制成的哨子，在使用的地方更容易遇到隐大的宝可梦。',
    type: 'flute'
  },
  {
    id: 10,
    icon: '🎵',
    chineseName: '白色玻璃哨',
    japaneseName: 'しろいビードロ',
    englishName: 'White Flute',
    description: '以白色玻璃制成的哨子，在使用的地方更容易遇到弱小的宝可梦。',
    type: 'flute'
  },
  {
    id: 11,
    icon: '🌱',
    chineseName: '速速肥',
    japaneseName: 'すくすくこやし',
    englishName: 'Growth Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 12,
    icon: '🌱',
    chineseName: '湿湿肥',
    japaneseName: 'しめじめこやし',
    englishName: 'Damp Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 13,
    icon: '🌱',
    chineseName: '久久肥',
    japaneseName: 'ながながこやし',
    englishName: 'Stable Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 14,
    icon: '🌱',
    chineseName: '粘粘肥',
    japaneseName: 'ねばねばこやし',
    englishName: 'Gooey Mulch',
    description: '培育树果时的肥料，但完全不适合丰缘地区的土壤，所以没什么效果。',
    type: 'mulch'
  },
  {
    id: 15,
    icon: '🌱',
    chineseName: '硕果肥',
    japaneseName: 'たわわこやし',
    englishName: 'Rich Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 16,
    icon: '🌱',
    chineseName: '吃惊肥',
    japaneseName: 'びっくりこやし',
    englishName: 'Surprise Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 17,
    icon: '🌱',
    chineseName: '劲劲肥',
    japaneseName: 'ぐんぐんこやし',
    englishName: 'Boost Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 18,
    icon: '🌱',
    chineseName: '超效肥',
    japaneseName: 'とんでもこやし',
    englishName: 'Amaze Mulch',
    description: '',
    type: 'mulch'
  },
  {
    id: 19,
    icon: '🍯',
    chineseName: '朱红色花蜜',
    japaneseName: 'くれないのミツ',
    englishName: 'Red Nectar',
    description: '朱红色花的花蜜，可以改变特定宝可梦的样子。',
    type: 'nectar'
  }
]

// 道具分类定义
const itemCategories = computed(() => {
  const categories = [
    { type: 'repel', name: '驱虫喷雾', icon: '🥫', count: 0 },
    { type: 'ball', name: '箱子道具', icon: '📦', count: 0 },
    { type: 'flute', name: '玻璃笛', icon: '🎵', count: 0 },
    { type: 'mulch', name: '肥料', icon: '🌱', count: 0 },
    { type: 'nectar', name: '花蜜', icon: '🍯', count: 0 },
  ]
  
  // 计算每个分类的道具数量
  categories.forEach(category => {
    category.count = itemsList.value.filter(item => item.type === category.type).length
  })
  
  // 只返回有道具的分类
  return categories.filter(category => category.count > 0)
})

// 计算属性
const totalCount = computed(() => filteredItemsList.value.length)

const paginatedItemsList = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return filteredItemsList.value.slice(start, end)
})

// 获取道具数据
const fetchItemsList = async () => {
  loading.value = true
  try {
    // 使用模拟数据
    await new Promise(resolve => setTimeout(resolve, 500)) // 模拟网络延迟
    
    itemsList.value = mockItemsData
    filteredItemsList.value = mockItemsData
    
    console.log(`成功获取 ${mockItemsData.length} 个道具数据`)
    // 移除了提示
  } catch (error) {
    // 移除了控制台提示
    // 移除了提示
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = () => {
  applyFilters()
}

// 应用筛选器
const applyFilters = () => {
  let filtered = [...itemsList.value]
  
  // 搜索筛选
  if (searchQuery.value.trim()) {
    const keyword = searchQuery.value.trim().toLowerCase()
    filtered = filtered.filter(item => {
      const chineseName = item.chineseName.toLowerCase()
      const japaneseName = item.japaneseName.toLowerCase()
      const englishName = item.englishName.toLowerCase()
      return chineseName.includes(keyword) || 
             japaneseName.includes(keyword) || 
             englishName.includes(keyword)
    })
  }
  
  // 默认按名称排序
  filtered.sort((a, b) => {
    return a.chineseName.localeCompare(b.chineseName, 'zh-CN')
  })
  
  filteredItemsList.value = filtered
}

// 重置筛选器
const resetFilters = () => {
  searchQuery.value = ''
  currentPage.value = 1
  filteredItemsList.value = [...itemsList.value]
}

// 分页处理
const handlePageChange = (page) => {
  currentPage.value = page
}

const handlePageSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
}

// 显示道具详情
const showItemDetail = (item) => {
  selectedItem.value = item
  dialogVisible.value = true
}

// 根据分类获取道具
const getItemsByCategory = (categoryType) => {
  return itemsList.value.filter(item => item.type === categoryType)
}

// 滚动到指定分类
const scrollToCategory = (categoryType) => {
  categoryDropdownOpen.value = false // 关闭下拉菜单
  
  nextTick(() => {
    const element = document.getElementById(`category-${categoryType}`)
    if (element) {
      element.scrollIntoView({ 
        behavior: 'smooth',
        block: 'start'
      })
      // 高亮显示该分类
      element.classList.add('category-highlight')
      setTimeout(() => {
        element.classList.remove('category-highlight')
      }, 2000)
    }
  })
}

// 监听搜索输入，实现实时搜索
watch(searchQuery, () => {
  if (searchQuery.value.trim() === '') {
    handleSearch()
  }
})

// 组件挂载时获取数据
onMounted(() => {
  fetchItemsList()
})
</script>

<style scoped>
.items-page {
  min-height: 100vh;
  background-image: url('@/assets/bk_img01.png');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  padding: 20px;
  position: relative;
}

.items-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(1px);
  z-index: 0;
}

.items-page > * {
  position: relative;
  z-index: 1;
}

/* 页面标题 */
.page-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 40px 20px;
}

.page-title {
  font-size: 3.5rem;
  font-weight: bold;
  color: #2c3e50;
  margin: 0 0 15px 0;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.3rem;
  color: #34495e;
  margin: 0;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}

/* 搜索和筛选区域 */
.search-filter-section {
  max-width: 1200px;
  margin: 0 auto 40px auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.search-bar {
  display: flex;
  gap: 15px;
  margin-bottom: 25px;
  align-items: center;
}

.search-bar .el-input {
  flex: 1;
  max-width: 500px;
}

.reset-btn {
  min-width: 80px;
  border: 2px solid #dcdfe6;
  transition: all 0.3s ease;
  color: white !important;
}

.reset-btn:hover {
  border-color: #c0c4cc;
  background-color: #f5f7fa;
}

/* 分类下拉菜单样式 */
.category-dropdown {
  position: relative;
  margin-right: 12px;
}

.category-dropdown-checkbox {
  display: none;
}

.category-dropdown-label {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: white;
  border: 2px solid #dcdfe6;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  min-height: 40px;
  user-select: none;
}

.category-dropdown-label:hover {
  border-color: #409EFF;
  background-color: #f0f9ff;
}

.category-dropdown-checkbox:checked + .category-dropdown-label {
  border-color: #409EFF;
  background-color: #409EFF;
  color: white;
}

.category-icon {
  margin-right: 6px;
  font-size: 16px;
}

.category-text {
  margin-right: 6px;
  font-weight: 500;
}

.dropdown-arrow {
  font-size: 12px;
  transition: transform 0.3s ease;
}

.dropdown-arrow-open {
  transform: rotate(180deg);
}

.category-dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 200px;
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  margin-top: 4px;
}

.category-menu-header {
  padding: 12px 16px;
  font-weight: 600;
  color: #303133;
  border-bottom: 1px solid #f0f0f0;
  background-color: #f8f9fa;
  border-radius: 8px 8px 0 0;
}

.category-menu-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.category-menu-item {
  border-bottom: 1px solid #f0f0f0;
}

.category-menu-item:last-child {
  border-bottom: none;
}

.category-link {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  text-decoration: none;
  color: #606266;
  transition: all 0.3s ease;
}

.category-link:hover {
  background-color: #f5f7fa;
  color: #409EFF;
}

.category-emoji {
  margin-right: 8px;
  font-size: 16px;
}

.category-name {
  flex: 1;
  font-weight: 500;
}

.category-count {
  font-size: 12px;
  color: #909399;
}

/* 分类区域样式 */
.category-sections {
  max-width: 1200px;
  margin: 0 auto;
}

.category-section {
  margin-bottom: 40px;
  transition: all 0.3s ease;
}

.category-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding: 12px 20px;
  background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
  color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.category-header .category-icon {
  font-size: 20px;
  margin-right: 12px;
}

.category-title {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.category-header .category-count {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

.category-highlight {
  transform: scale(1.02);
  box-shadow: 0 4px 20px rgba(64, 158, 255, 0.4);
}

/* 结果信息 */
.result-info {
  max-width: 1200px;
  margin: 0 auto 20px auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #2c3e50;
  font-size: 1rem;
  font-weight: 600;
  text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.8);
}

.result-count strong {
  color: #e74c3c;
}

/* 表格容器 */
.items-table-container {
  max-width: 1200px;
  margin: 0 auto 40px auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

:deep(.items-table) {
  border-radius: 15px;
  overflow: hidden;
}

:deep(.items-table .el-table__header) {
  border-radius: 15px 15px 0 0;
}

:deep(.items-table .el-table__row) {
  cursor: pointer;
  transition: all 0.3s ease;
}

:deep(.items-table .el-table__row:hover) {
  background-color: rgba(64, 158, 255, 0.1) !important;
}

.item-icon {
  font-size: 1.5rem;
}

.description-text {
  line-height: 1.5;
  color: #555;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #2c3e50;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  max-width: 600px;
  margin: 0 auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 20px;
}

.empty-state h3 {
  font-size: 1.5rem;
  margin: 0 0 10px 0;
}

.empty-state p {
  margin: 0 0 20px 0;
  opacity: 0.8;
}

/* 分页 */
.pagination-section {
  display: flex;
  justify-content: center;
  padding: 40px 20px;
}

:deep(.el-pagination) {
  --el-pagination-bg-color: rgba(255, 255, 255, 0.9);
  --el-pagination-text-color: #333;
  --el-pagination-border-radius: 10px;
}

/* 详情对话框 */
.item-detail {
  padding: 20px;
}

.detail-header {
  display: flex;
  gap: 20px;
  margin-bottom: 25px;
  align-items: center;
}

.detail-icon {
  font-size: 3rem;
  flex-shrink: 0;
}

.detail-basic {
  flex: 1;
}

.detail-name {
  font-size: 1.8rem;
  font-weight: bold;
  color: #333;
  margin: 0 0 15px 0;
}

.detail-names p {
  margin: 5px 0;
  color: #666;
}

.detail-description {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 10px;
  margin-bottom: 20px;
}

.detail-description h4 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 1.1rem;
}

.detail-description p {
  margin: 0;
  line-height: 1.6;
  color: #555;
}

.reset-primary-btn {
  color: white !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-title {
    font-size: 2.5rem;
  }
  
  .search-bar {
    flex-direction: column;
  }
  
  .result-info {
    flex-direction: column;
    gap: 10px;
    text-align: center;
  }
  
  .items-table-container {
    margin: 0 10px 20px 10px;
    padding: 15px;
  }
  
  .detail-header {
    flex-direction: column;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .search-filter-section {
    margin: 0 10px 20px 10px;
    padding: 20px;
  }
  
  .items-table-container {
    padding: 10px;
  }
  
  :deep(.el-table .el-table__cell) {
    padding: 8px 4px;
  }
  
  .description-text {
    font-size: 0.9rem;
  }
}
</style>